package com.ashin.controller;

import com.ashin.bean.User;
import com.ashin.service.UserService;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.math.BigDecimal;

/**
 * @Author: Ashin
 * @Date: 2021/4/13
 * @Description: com.ashin.controller
 * @Version: 1.0.0
 */
@RequestMapping("/movies")
@RestController
public class MovieController {

    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private UserService userService;

    @HystrixCommand(fallbackMethod = "findByIdFallback")
    @GetMapping("/users/{id}")
    public User findById(@PathVariable Long id) {
        // 这里用到了RestTemplate的占位符能力
        User user = this.restTemplate.getForObject("http://test-sc-provider-user/users/{id}", User.class, id);
        // ...电影微服务的业务...
        return user;
    }

    public User findByIdFallback(Long id) {
        return new User(id, "默认用户", "默认用户", 0, new BigDecimal(1));
    }

    @GetMapping("/feign/users/{id}")
    public User findById2(@PathVariable Long id) {
        // 这里用到了RestTemplate的占位符能力
        User user = userService.findById(id);
        // ...电影微服务的业务...
        return user;
    }
}
